﻿using MediatR;
using Swork03_28.Api.Application.Commands.TraceableCommand;
using Swork03_28.Services.Interface;

namespace Swork03_28.Api.Application.CommandHandlers.TraceableHandler
{
    public class TraceableDeleteCommandHandler : IRequestHandler<TraceableDeleteCommand, int>
    {
        private readonly ITraceableService _traceableService;

        public TraceableDeleteCommandHandler(ITraceableService traceableService)
        {
            _traceableService = traceableService;
        }

        public async Task<int> Handle(TraceableDeleteCommand request, CancellationToken cancellationToken)
        {
            var id = request.Ids.Split(",");
            var dels = await _traceableService.GetValues(x => id.Contains(x.Id.ToString()));
            var count = 0;
            foreach (var item in dels)
            {
                item.IsDel = true;
                count += await _traceableService.UpdateAsync(item);
            }
            return count;
        }
    }
}
